import 'intersection-observer'
import Vue from 'vue'
import App from '@/App.vue'
import router from '@/router'
import store from '@/store'
import i18n from '@/plugins/i18n'
import api from '@/service/api.js'
import openAPI from '@/service/index.js'
import Buefy from 'buefy'
import VueFullscreen from 'vue-fullscreen'
import Vue2TouchEvents from 'vue2-touch-events'
import VueSocialSharing from 'vue-social-sharing'
import VueSocketIOExt from 'vue-socket.io-extended';
import messageBus from '@/events/index.js'
import VueDOMPurifyHTML from 'vue-dompurify-html'
// import { login } from '@/utils/login.js'
// localStorage.setItem('lang', 'zh_cn')

// Import Styles
import '@/assets/scss/app.scss'
import VAnimateCss from 'v-animate-css';
import { logined } from './utils/login'

const io = require("socket.io-client");

const isDev = process.env.NODE_ENV === 'dev';
const protocol = document.location.protocol
const wsProtocol = protocol === 'https:' ? 'wss:' : 'ws:'
const devIp = process.env.VUE_APP_DEV_IP
const devPort = process.env.VUE_APP_DEV_PORT
const localhost = document.location.host
const localhostName = document.location.hostname
const baseIp = isDev ? `${devIp}` : `${localhostName}`
// const baseURL = isDev ? `${devIp}:${devPort}` : `${localhost}`
let port = JSON.parse(localStorage.getItem('port'))
const baseURL = `47.94.1.69:${port+1}`
const wsURL = `${wsProtocol}//${baseURL}`

const socket = io(wsURL, {
  transports: ['websocket', 'polling'],
  path: '/v2/message_bus/socket.io/',
});

Vue.use(Buefy)
Vue.use(VueFullscreen)
Vue.use(VAnimateCss, { animateCSSPath: '/css/animate.min.css' });
Vue.use(Vue2TouchEvents)
Vue.use(VueSocketIOExt, socket);
Vue.use(VueSocialSharing);
Vue.use(VueDOMPurifyHTML, {
  default: {
    ALLOWED_ATTR: ['target', 'href']
  }
});

Vue.config.productionTip = false
Vue.prototype.$api = api;
Vue.prototype.$openAPI = openAPI;
Vue.prototype.$baseIp = baseIp;
Vue.prototype.$baseURL = baseURL;
Vue.prototype.$protocol = protocol;
Vue.prototype.$wsProtocol = wsProtocol;


// Create an EventBus
Vue.prototype.$EventBus = new Vue();
Vue.prototype.$messageBus = messageBus;
new Vue({
  router,
  i18n,
  store,
  render: h => h(App)
}).$mount('#app')

window.addEventListener('message', async function (event) {
	if (event.origin === "http://127.0.0.1:5173") {
  // if (event.origin === "http://ts.aipocket.igrsservice.com" || event.origin === "http://ts.aipocket-web.igrsservice.com") {
	  if (event.data.message === 'login') {
		  console.log(event.data)
		  localStorage.setItem('device', JSON.stringify(event.data))
		  await logined(event.data)
		  event.source.postMessage('logined', '*');
	  }
  }
}, false);